<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title> 学生录入的医案 </title>
  <link rel="stylesheet" type="text/css" href="/css/style.css" />

  <style type="text/css">
    *, body {
      margin: 0px;
      font-family: '微软雅黑';
    }
    .button-container {
      float: right;
    }
  </style>
  <script src="/js/jquery-1.10.2.min.js"></script>
  <script src="/js/generic.js"></script>
  <script src="/js/fypage.js"></script>
</head>

<body>


<table class="maintable"style="width:100%">
  <tr>
    <td class="rightmaintd" valign="top">
      <div class="searchdiv">
        <input type="button" id="btnAdd" style="display: none;" value="+添加医案" class="xinzengbtn" onclick="create()">

        <div style="float: right;">

          <form action="/yian/export2Excel" method="post">
<!--            <input type="submit" value="导出详细医案信息" class="chaxunbtn" style="float:left; width: 150px" />-->
            <input type="button" value="导出当前表格数据" class="chaxunbtn" onclick="table2Excel('医案信息')" style="float:left; width: 150px" />
            &nbsp;&nbsp;
            <label>&nbsp;年级：&nbsp;</label>
            <select id="gradeSelect" onchange="changeGradeFun();">
              <option value="" id="gradeNone">请选择</option>
              <option th:each="Id : ${yonghuId}" th:value="${Id}" th:text="${Id}">Id</option>
            </select>
            <label>&nbsp;班级：&nbsp;</label>
            <select id="classSelect" onchange="changeClassFun();">
              <option value="" id="classNone">请选择</option>
              <option th:each="Cop : ${yonghuCompany}" th:value="${Cop}" th:text="${Cop}">Id</option>
            </select>
            <button style="float:right"><img src="/images/fdj.png" alt="" class="searchfdj"></button>
            <input type="button" value="查询医案" class="chaxunbtn" onclick="search();">
          </form>

        </div>

      </div>
      <div style="height:30px">

      </div>
      <table class="neirontbiao" id="tbl_list"style="width:100%"></table>
      <div class="button-container">
        <button id="prevButton">上一页</button>
        <button id="nextButton">下一页</button>
      </div>
      每页显示条数：
      <select id="pageSizeSelect" onchange="changePageSize();">
        <option value="10">10</option>
        <option value="25">25</option>
        <option value="50">50</option>
        <option value="100" selected>100</option>
        <option value="200">200</option>
      </select>
      <div class="pagediv" id="pager">
        <label>第
          <input type="text" id="page" width="5px" onkeypress="pageSearch(event)">
          页
        </label>
        <input type="button" value="跳转" onclick="pageOn()">
        <p id="sumPageId">place</p>
      </div>

    </td>
  </tr>
</table>
<p class="shownone" id="Userrole" th:text="${roleType}"></p>
<!--userid用户类型-->

</body>
</html>
<script type="text/javascript">
    var role;
    var roleCheck = true;
    usercheckFun();
    function usercheckFun(){
        role = document.getElementById('Userrole').textContent;
        if(role=='二级审核'||role=='教师审核'||role=='名医专家'||role=='学生'||role=='输入者'){
            roleCheck = false;
        }
    }
  function start(){
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/xslr/', true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4 && xhr.status === 200) {
        dataSource = JSON.parse(xhr.responseText);
        dataSource.forEach(function(obj) {
          for (var key in obj) {
            if (obj.hasOwnProperty(key) && obj[key] === null) {
              obj[key] = ''; // 将属性值为 null 的属性替换为空字符串
            }
          }
        });
        data = JSON.parse(JSON.stringify(dataSource));
        renderData();
      }
    };
    xhr.send();
  }


  var changeGrade=document.getElementById('gradeSelect');
  var changeClass=document.getElementById('classSelect');

  var pageChage=document.getElementById('page');
  var sumPageShow=document.getElementById('sumPageId');
  var pageSize = 50; // 每页显示的数据量
  var currentPage = 1; // 当前页码
  var dataRender=[];  //每页显示的数据，是数据
  var pageSum=0;  //总共的页数
  var dataSource=[];   //一共有的json数据，源数据
  var data=[];  // 操作的数据，防止错误，可以由源数据赋值
  // 使用AJAX请求获取JSON数据
  start();
  function renderData(){    //渲染页面函数
    pageSum=Math.ceil(data.length/(pageSize));
    dataRender=data.slice((currentPage-1)*pageSize,pageSize*currentPage);
    var container = document.getElementById('tbl_list');
    var html = '<tr class="neirontbiaotou"><th width="7%">患者姓名</th><th width="5%">性别</th><th width="5%">年龄</th><th width="14%" class="noExl">就诊时间</th><th class="shownone">就诊日期：年</th><th class="shownone">月</th><th class="shownone">日</th><th width="9%">医生</th><th width="10%" >书名</th><th width="5%">起始页</th><th width="5%">终止页</th><th width="12%">录入人</th><th width="7%">审核者</th><th width="6%">状态</th><th class="noExl">操作</th></tr>';
    if (dataRender.length == 0) {
      html += '<tr><td colspan="12" style="text-align:center;height:50px;">暂无医案信息</td></tr>';
    }
    for (var i = 0; i < dataRender.length; i++) {
      var item = dataRender[i];
      html += '<tr>';
      html += '<td>' + item.xingming + '</td>';
      html += '<td>' + item.xingbie + '</td>';
      html += '<td>' + item.nianling + '</td>';
      html += '<td class="noExl">' + formDate1(item.year,item.month,item.day) + '</td>';
        html += '<td class="shownone">' + item.year + '</td>';
        html += '<td class="shownone">' + item.month + '</td>';
        html += '<td class="shownone">' + item.day + '</td>';
      html += '<td>' + item.jiuzhenyisheng + '</td>';
      html += '<td>' + item.shuming + '</td>';
      html += '<td>' + item.yema + '</td>';
      html += '<td>' + item.yemaEnd + '</td>';
      html += '<td>' + item.createuser + '</td>';
      html += '<td>' + item.secondcheckuser + '</td>';
      html += '<td>' + item.shenhe + '</td>';
      // html += '<td class="noExl">' + '<a href="javascript:update(\'' + item.id + '\')">编辑</a>&nbsp;<a href="javascript:del(\'' + item.id + '\')">删除</a></br><a href="javascript:repulse(\'' + item.id + '\')">退回</a>' + '</td></tr>';
        if(roleCheck){
            html += '<td class="noExl">' + '<a href="javascript:update(\'' + item.id + '\')">编辑</a>' ;
            html += '&nbsp;<a style=";color:#0c531f" href="javascript:deleteD(\'' + item.id + '\')">删除</a>';
            html +='<a href="javascript:repulse(\'' + item.id + '\')">退回</a>';
        }
        else if (item.shenhe=='保存'){
            html += '<td class="noExl">' + '<a href="javascript:update(\'' + item.id + '\')">编辑</a>' ;
            html += '&nbsp;<a style=";color:#0c531f" href="javascript:deleteD(\'' + item.id + '\')">删除</a>';

        }
        else html += '<td class="noExl">' + '<a href="javascript:update(\'' + item.id + '\')">查看</a>' ;
        html += '</td></tr>'
    }
    pageChage.value=currentPage;
    sumPageShow.textContent="共 "+pageSum+" 页";
    container.innerHTML = html;
  }
    function formDate1(year,month,day){
        if(year==''){
            if(month==''){
                if(day=='') return '';
                else return `${day}日`;
            }
            else {
                if(day=='') return `${month}月`;
                else return `${month}月${day}日`;
            }
        }
        else {
            if(month==''){
                if(day=='') return `${year}年`;
                else return `${year}年${day}日`;
            }
            else {
                if(day=='') return `${year}年${month}月`;
                else return `${year}年${month}月${day}日`;
            }
        }
    }
  function nextOrPreOnClick(){
    //上一页
    var prevButton = document.getElementById('prevButton');
    prevButton.addEventListener('click', function() {
      currentPage--;
      if(currentPage==0)
        currentPage=pageSum;
      renderData();

    });

    // 下一页按钮点击事件处理函数
    var nextButton = document.getElementById('nextButton');
    nextButton.addEventListener('click', function() {
      currentPage=(++currentPage)%pageSum;
      currentPage=currentPage==0?pageSum:currentPage;
      renderData();
    });
  }
  function changePageSize(){
    var pageSizeSelect = document.getElementById('pageSizeSelect');
    pageSize = parseInt(pageSizeSelect.value);
    currentPage=1;
    renderData();
  }
  nextOrPreOnClick();
  function enterKeyPress(event) {
    // 检查按下的按键是否为回车键，回车键的keyCode是13
    if (event.keyCode === 13) {
      search();
    }
  }
  function update(userid){
    window.open("/wxya/edit/" + userid);
  }
  function repulse(userid){
    var result=confirm("确认退回医案重新修改吗？");
    if(result){
      // 发送 DELETE 请求
      fetch('/xslr/'+userid, {
        method: 'PUT',
        headers: {
          'Content-Type': 'application/json' // 可根据实际情况设置请求头
        },
      })
              .then(response => {
                if (response.ok) {
                  alert("退回成功！");
                  start();
                  renderData();
                } else {
                  alert("发生错误，退回失败！");
                }
              })
              .catch(error => {
                // 捕获网络错误等异常
                alert("网络错误，退回失败！");
              });

    }
  }
  function deleteD(userid){
    var result=confirm("确认删除当前内容吗？");
    if(result){
      // 发送 DELETE 请求
      fetch('/wxya/'+userid, {
        method: 'DELETE',
        headers: {
          'Content-Type': 'application/json' // 可根据实际情况设置请求头
        },
      })
              .then(response => {
                if (response.ok) {
                  alert("删除成功！");
                  start();
                  renderData();
                } else {
                  alert("发生错误，删除失败！");
                }
              })
              .catch(error => {
                // 捕获网络错误等异常
                alert("网络错误，删除失败！");
              });

    }
  }
  function pageSearch(event) {
    // 检查按下的按键是否为回车键，回车键的keyCode是13
    if (event.keyCode === 13) {
      pageOn();
    }
  }
  function pageOn(){
    var afterPage=pageChage.value;
    if(afterPage<1||afterPage>pageSum){
      alert("输入的页码错误！");
      return;
    }
    currentPage=afterPage;
    renderData();
  }
  function changeGradeFun(){
    var val=changeGrade.value.toUpperCase();
    currentPage = 1;
    data = [];
    if (val== "") {
      data = JSON.parse(JSON.stringify(dataSource));
    }
    else {
      for (var i = 0; i < dataSource.length; i++) {
        if (dataSource[i].createuser.substring(0,4).toUpperCase().includes(val) ) {
          data.push(dataSource[i]);
        }
      }
    }
    document.getElementById('classNone').selected=true;
    renderData();
  }
  function changeClassFun(){
    var val=changeClass.value;
    currentPage = 1;
    data = [];

    if (val== "") {
      data = JSON.parse(JSON.stringify(dataSource));
    }
    else {
      for (var i = 0; i < dataSource.length; i++) {
        if (dataSource[i].company.toUpperCase().includes(val) ) {
          data.push(dataSource[i]);
        }
      }
    }
    document.getElementById('gradeNone').selected=true;
    renderData();
  }
  // 2020-11-12 修改，导出表格中的简短信息到Excel中。利用Jquery的table2excel。
  function table2Excel(filename) {
    $("#tbl_list").table2excel({
      exclude: ".noExl",//class="noExl"的列不导出
      name: "Excel Document Name",
      filename: filename  + ".xlsx",//文件名称
      fileext: ".xlsx",//文件后缀名
      exclude_img: true,//导出图片
      exclude_links: true,//导出超链接
      exclude_inputs: true//导出输入框内容
    });
  }
</script>
<script src="/js/jquery.table2excel.js"></script>